import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/Login.vue'
import Main from '@/views/Main'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/',
    redirect: '/login'
  },{
    path: '/main',
    redirect: '/homepage'
  },
  {
    path:'/login',
    name:'login',
    component: login
  },
  {
    path: '/main',
    name :'main',
    component: Main,
    children:[
      {
        path:'/homepage',
        name:'homepage',
        component:()=>import ('../views/homepage')
      },
      {
        path:'/user',
        name:'user',
        component:()=>import ('../views/user')
      },
      {
        path:'/setting',
        name:'setting',
        component:()=>import ('../views/setting')
      },
      {
        path:'/basedata/supplier',
        name:'supplier',
        component:()=>import ('../views/basedata/supplier')
      },
      {
        path:'/basedata/stock',
        name:'stock',
        component:()=>import ('../views/basedata/stock')
      },{
        path:'/basedata/other',
        name:'other',
        component:()=>import ('../views/basedata/other')
      },
      {
        path:'/basedata/goods',
        name:'goods',
        component:()=>import ('../views/basedata/goods')
      },{
        path:'/basedata/customer',
        name:'customer',
        component:()=>import ('../views/basedata/customer')
      },
      {
        path:'/statistics/enterstatistics',
        name:'supplierstatistics',
        component:()=>import ('../views/statistics/supplierstatics')
      },
      {
        path:'/statistics/customerstatistics',
        name:'customerstatistics',
        component:()=>import ('../views/statistics/customerstatistics')
      },
      {
        path:'/statistics/goodsstatistics',
        name:'goodsstatistics',
        component:()=>import ('../views/statistics/goodsstatistics')
      },
      {
        path:'/purchasemanage/purchaseplan',
        name:'purchaseplan',
        component:()=>import ('../views/purchaseManage/purchasementPlan')
      },
      {
        path:'/purchasemanage/purchaseorder',
        name:'purchaseorder',
        component:()=>import ('../views/purchaseManage/purchaseOrder')
      },
      {
        path:'/stockmanage/enterstock',
        name:'enterstock',
        component:()=>import ('../views/stockManage/enterStock')
      },
      {
        path:'/stockmanage/cancelstock',
        name:'cancelstock',
        component:()=>import ('../views/stockManage/cancelStock')
      },
      {
        path:'/stockmanage/checkoutstock',
        name:'checkoutstock',
        component:()=>import ('../views/stockManage/checkoutStock')
      },
      {
        path:'/salesmanage/salesdelivery',
        name:'salesdelivery',
        component:()=>import ('../views/salesManage/salesDelivery')
      },
      {
        path:'/salesmanage/salesreturn',
        name:'salesreturn',
        component:()=>import ('../views/salesManage/salesReturn')
      },
      {
        path:'/financial/shouldpay',
        name:'shouldpay',
        component:()=>import ('../views/financialManage/shouldPay')
      },
      {
        path:'/financial/shouldreceive',
        name:'shouldreceive',
        component:()=>import ('../views/financialManage/shouldReceive')
      },{
        path:'/personal',
        name:'personal',
        component:()=>import ('../views/personal.vue')
      },
    ]
  }

]

const router = new VueRouter({
  routes
})
//路由守卫
router.beforeEach((to, from, next) => {
  if (to.path.startsWith('/login')) {
      window.sessionStorage.removeItem('token')
      next()
  } else {
      let user = window.sessionStorage.getItem('token')
      if (!user) {
          next({
              path: '/login'
          })
      } else {
          next()
      }
  }
});
export default router
